<template>
  <div>
    <el-row>
      <el-col>
        <el-form :inline="true" :model="formInline">
          <el-form-item>
            <el-input size="mini" v-model="formInline.num" placeholder="请输入设备编号"></el-input>
          </el-form-item>
          <el-form-item>
            <el-input size="mini" v-model="formInline.name" placeholder="请输入资产名称"></el-input>
          </el-form-item>
          <el-form-item>
            <el-select clearable size="mini" v-model="formInline.classificationId" placeholder="请选择归类">
              <el-option
                      v-for="item in options"
                      :key="item.ID"
                      :label="item.NAME"
                      :value="item.ID">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-select clearable size="mini" v-model="formInline.status" placeholder="请选择状态">
              <el-option label="启用" value="1"></el-option>
              <el-option label="禁用" value="0"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" size="mini" @click="search">查询</el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
    <el-row class="buttonList">
      <el-col>
        <!--<el-button type="primary" size="mini">导入</el-button>-->
        <el-button type="primary" size="mini" :disabled="btnDisabled" @click="view">查看详情</el-button>
        <el-button type="primary" size="mini" :disabled="btnDisabled" @click="setting">设置设备归类</el-button>
        <el-button type="primary" size="mini" :disabled="btnDisabled" @click="saoma">二维码</el-button>
        <el-button type="danger" size="mini">启/禁用</el-button>
      </el-col>
    </el-row>
    <el-row>
      <el-col :span="4">
        <el-tree accordion highlight-current :data="data" :props="defaultProps" @node-click="handleNodeClick"></el-tree>
      </el-col>
      <el-col :span="20">
        <el-table
          :data="tableData"
          size="mini"
          @selection-change="handleSelectionChange"
          border
          style="width: 100%">
          <el-table-column
                  align="center"
                  type="selection"
                  width="55">
          </el-table-column>
          <el-table-column
            align="center"
            prop="NUM"
            label="设备编号">
          </el-table-column>
          <el-table-column
            align="center"
            prop="NAME"
            width="100px"
            label="资产名称">
          </el-table-column>
          <el-table-column
                  align="center"
                  prop="CLASSIFICATIONNAME"
                  label="归类属性">
          </el-table-column>
          <el-table-column
            align="center"
            prop="AMOUNT"
            label="单价/元">
          </el-table-column>
          <!--<el-table-column
            align="center"
            prop="SPECS"
            label="规格">
          </el-table-column>-->
          <el-table-column
            align="center"
            prop="MODEL"
            width="180px"
            label="型号">
          </el-table-column>
          <!--<el-table-column
            align="center"
            prop="TYPENUM"
            label="分类号">
          </el-table-column>-->
            <el-table-column
                  align="center"
                  prop="COMPANYNAME"
                  label="单位名称">
            </el-table-column>
            <!--<el-table-column
                    align="center"
                    prop="COMPANY"
                    label="使用单位">
            </el-table-column>-->
            <el-table-column
                    align="center"
                    prop="DIRECTIONNAME"
                    label="使用方向">
            </el-table-column>
          <!--<el-table-column
                  align="center"
                  prop="TYPENUM"
                  label="分类号">
          </el-table-column>-->
          <el-table-column
                  align="center"
                  prop="STATUS"
                  label="状态">
            <template slot-scope="scope">
              {{scope.row.STATUS ? '启用': '禁用'}}
            </template>
          </el-table-column>
          <!--<el-table-column
            align="center"
            prop="ISVALUABLE"
            label="是否贵重">
            <template slot-scope="scope">
              {{scope.row.ISVALUABLE ? '是': '否'}}
            </template>
          </el-table-column>
            <el-table-column
                    align="center"
                    prop="ISCONSUMABLES"
                    label="是否易耗">
              <template slot-scope="scope">
                {{scope.row.ISCONSUMABLES ? '是': '否'}}
              </template>
            </el-table-column>
            <el-table-column
                    align="center"
                    prop="ISNORMAL"
                    label="是否正常">
              <template slot-scope="scope">
                {{scope.row.ISNORMAL ? '是': '否'}}
              </template>
            </el-table-column>
            <el-table-column
                    align="center"
                    prop="ISAPPOINTMENT"
                    label="预约">
              <template slot-scope="scope">
                {{scope.row.ISAPPOINTMENT ? '是': '否'}}
              </template>
            </el-table-column>
            <el-table-column
                    align="center"
                    prop="ISRECEIVE"
                    label="领用">
              <template slot-scope="scope">
                {{scope.row.ISRECEIVE ? '是': '否'}}
              </template>
            </el-table-column>-->
          <el-table-column
                  align="center"
                  prop="PRESENTNAME"
                  label="现状">
          </el-table-column>
          <!--<el-table-column
                  align="center"
                  prop="COUNTRY"
                  label="国别">
          </el-table-column>
            <el-table-column
                    align="center"
                    prop="SOURCENAME"
                    label="来源">
            </el-table-column>-->
        </el-table>
        <el-pagination
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
                background
                layout="total, sizes, prev, pager, next, jumper"
                :page-size="pagination.pageSize"
                :total="pagination.total"
                :page-sizes="[10, 20, 30, 50, 100]"
        >
        </el-pagination>
      </el-col>
    </el-row>
    <el-dialog title="设备归类属性" :visible.sync="dialogFormVisible" center width="30%">
      <el-form :model="form">
        <el-row>
          <el-col :span="24">
            <el-form-item label="归类属性" :label-width="formLabelWidth">
              <el-select clearable style="width: 100%" size="mini" v-model="value" placeholder="请选择归类">
                <el-option
                        v-for="item in options"
                        :key="item.ID"
                        :label="item.NAME"
                        :value="item.ID">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="mini" @click="dialogFormVisible = false">取 消</el-button>
        <el-button size="mini" type="primary" @click="submit">确 定</el-button>
      </div>
    </el-dialog>
    <el-dialog v-if="dialogFormVisible2" title="二维码" :visible.sync="dialogFormVisible2" center width="80%">
      <div class="station" style="display: flex; flex-wrap: wrap" ref="stationURl"></div>
      <div slot="footer" class="dialog-footer">
        <el-button size="mini" @click="dialogFormVisible2 = false">取 消</el-button>
        <el-button size="mini" type="primary" @click="print">确 定</el-button>
      </div>
    </el-dialog>
    <el-dialog title="设备详情" :visible.sync="dialogFormVisible3" center width="50%">
      <el-form :model="viewData" :label-width="formLabelWidth">
        <el-row>
          <el-col :span="12">
            <el-form-item label="设备编号">
              {{viewData.LINECODE}}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="资产名称">
              {{viewData.NAME}}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="归类属性">
              {{viewData.CLASSIFICATIONNAME}}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="设备单价">
              {{viewData.AMOUNT + '/元'}}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="设备规格">
              {{viewData.SPECS}}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="设备型号">
              {{viewData.MODEL}}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="分类号">
              {{viewData.TYPENUM}}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="设备状态">
              {{viewData.STATUS ? '启用' : '禁用'}}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="是否贵重">
              {{viewData.ISVALUABLE ? '是' : '否'}}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="是否易耗">
              {{viewData.ISCONSUMABLES ? '是' : '否'}}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="是否正常">
              {{viewData.ISNORMAL ? '是' : '否'}}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="是否预约">
              {{viewData.ISAPPOINTMENT ? '是' : '否'}}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="是否领用">
              {{viewData.ISRECEIVE ? '是' : '否'}}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="设备现状">
              {{viewData.PRESENTNAME}}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="国别">
              {{viewData.COUNTRY}}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="设备来源">
              {{viewData.SOURCENAME}}
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="mini" @click="dialogFormVisible3 = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import QRCode from 'qrcodejs2'
  export default {
    name: "document",
    data() {
      return{
        pagination: {
          pageSize: 20,
          total: 1,
          pageNo: 1
        },
        form: {},
        value: '',
        options: [],
        dialogFormVisible: false,
        dialogFormVisible2: false,
        dialogFormVisible3: false,
        viewData: {},
        data: [],
        defaultProps: {
          children: 'list',
          label: 'NAME'
        },
        formInline: {
          departmentId: '',
          testBaseId: '',
          testId: ''
        },
        tableData: [],
        tableVal: [],
        btnDisabled: true,
        formLabelWidth: '80px'
      }
    },
    created() {
      this.getData(this.pagination.pageSize, this.pagination.pageNo);
      this.getTreeData();
      this.$post('/insrtumentClassification/findInstrumentClassification').then(res => {
        this.options = res.data.data.dataList;
      })
    },
    methods: {
      view() {
        this.dialogFormVisible3 = true;
        this.viewData = this.tableVal[0];
      },
      saoma() {
        for(let i = 0; i < this.tableVal.length; i++) {
          this.dialogFormVisible2 = true;
          this.$nextTick(() => {
            var stationURl = new QRCode(this.$refs.stationURl, {
              text: this.tableVal[i].ID + '',
              width: 200,
              height: 200,
              colorDark: '#000000',
              colorLight: '#ffffff',
              correctLevel: QRCode.CorrectLevel.H
            })
          })
        }
      },
      print() {
        window.print()
      },
      search() {
        this.formInline.pageSize = this.pagination.pageSize;
        this.formInline.pageNo = 1;
        this.$post('/insrtument/findInstrument', this.formInline).then(res => {
          if(res.data.status != 1) {
            this.$message.error('条件输入错误，请检查');
            return;
          }
          this.pagination.total = res.data.data.total;
          this.tableData = res.data.data.dataList;
        })
      },
      getTreeData() {
        this.$post('/public/findStructureByTestBase').then(res => {
          this.data = res.data.data;
        })
      },
      getData(pageSize, pageNo) {
        this.$post('/insrtument/findInstrument', {pageSize, pageNo}).then(res => {
          this.tableData = res.data.data.dataList;
          this.pagination.total = res.data.data.total;
        })
      },
      setting() {
        this.dialogFormVisible = true;
      },
      submit() {
        let arr = [];
        for(let i = 0; i < this.tableVal.length; i++) {
          arr.push(this.tableVal[i].ID)
        }
        this.$post('insrtument/batchUpdateClassificationId', {classificationId: this.value, id: arr.join()}).then(res => {
          if(res.data.status === 1) {
            this.$message.success('设置成功');
            this.getData(this.pagination.pageSize, this.pagination.pageNo);
            this.dialogFormVisible = false;
          } else {
            this.$message.error('请求失败,请稍后再试');
          }
        })
      },
      /*updateStatus() {
        this.$post('/insrtument/update', {id:})
      },*/
      handleSizeChange(size) {
        this.pagination.pageSize = size;
        this.getData(size, this.pagination.pageNo);
      },
      handleCurrentChange(page) {
        this.pagination.pageNo = page;
        this.getData(this.pagination.pageSize, page);
      },
      handleSelectionChange(val) {
        if (val.length > 0) {
          this.btnDisabled = false;
        }else{
          this.btnDisabled = true;
        }
        this.tableVal = val;
      },
      handleNodeClick(data, node) {
        console.log(data);
        this.formInline.departmentId = '';
        this.formInline.testBaseId = '';
        this.formInline.testId = '';
        if(node.level === 1) {
          this.formInline.departmentId = data.ID;
        }
        if(node.level === 2) {
          this.formInline.testBaseId = data.ID;
        }
        if(node.level === 3) {
          this.formInline.testId = data.ID;
        }
        this.search();
      }
    }
  }
</script>

<style scoped lang="scss">
  .search{
    margin-bottom: 10px;
  }
  /deep/.el-select{
    width: 100%;
  }
  .addDev {
    /deep/.el-form-item__content {
      display: flex;
    }
  }
  /deep/.el-divider{
    background: #409eff;
  }
  /deep/.el-divider__text{
    font-size: 18px;
    color: #409eff;
  }
  /deep/.el-date-editor.el-input, .el-date-editor.el-input__inner {
    width: 100%
  }
  .station {
    /deep/ img {
      margin: 20px;
    }
  }
</style>
